//
//设备报修
//
<template>
  <div class="tenantMenu_main main_container">
    <div class="main_header">
      <div class="padding_container">
        <div class="left"><Breadcrumb /></div>
        <div class="right">
          <HeaderOperation :parent="this" />
        </div>
      </div>
    </div>
    <div v-loading="loading" class="main_main">
      <search-bar-new :parent="this" />
      <div ref="table_container" class="table_container">
        <el-table
          v-if="tableColumns.length>0"
          ref="table"
          :data="tableData"
          stripe
          border
          :height="tableHeight"

          :cell-style="cellStyle"
          @cell-click="cellClick"
          @sort-change="(column)=>{sortChange(column, this)}"
          @selection-change="(val)=>{selectionChange(val, this)}"
        >
          <el-table-column type="selection" width="70" align="center" />
          <Column v-for="(column,index) in tableColumns" :key="index" :prop="column.prop" :is-show="column.isShow" :label="column.label" :sortable="column.sortable" :width="column.width" :min-width="column.minWidth" />
          <el-table-column
            label="故障情况拍照"
            min-width="120"
          >
            <template slot-scope="scope">
              <FileManage
                :file-names="scope.row.docName"
                :file-real-names="scope.row.docName"
                :file-urls="scope.row.docUrl"
                :readonly="true"
                class="accessory"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="附件"
            min-width="120"
          >
            <template slot-scope="scope">
              <FileManage
                :file-names="scope.row.fileName"
                :file-real-names="scope.row.fileRealName"
                :file-urls="scope.row.fileUrl"
                :readonly="true"
                class="accessory"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" fixed="right" width="60">
            <template slot-scope="scope">
              <div>
                <el-button
                  v-if="scope.row.status < 2"
                  v-hasPermi="['device:repairReport:delete']"
                  type="text"
                  @click.stop="deleteOne(scope.row)"
                >删除</el-button>
                <button v-else class="el-button--text button_grey">删除</button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <div class="main_bottom">
      <pagination
        :total="total"
        :page.sync="pageData.pageNum"
        :limit.sync="pageData.pageSize"
        :selected-num="selectionsAll.length"
        @pagination="getList"
      />
    </div>

  </div>
</template>

<script>
import { list, getInfo, remove } from '@/api/equipmentManage/equipmentRepair/equipmentReportRepair'
import { getEquipmentList1 } from '@/api/equipmentManage/baseInfo/equipmentInfo'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面
import { getDictSelects } from '@/api/common/common'
import equipmentShow from './equipmentShow' // 查看页面
import { mapGetters } from 'vuex'

export default {
  name: 'Index',
  data() {
    return {
      loading: true,
      pageId: '设备报修', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: 'updateTime',
        isAsc: 'desc'
      },
      typeData: {
        status: 1
      },
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      defaultTableColumns: [// 表格常规字段（初始值）
        { prop: 'statusName', label: '报修单状态', canOrder: false },
        { prop: 'code', label: '报修单号' },
        { prop: 'bookName', label: '报修设备', minWidth: '200', canOrder: false },
        { prop: 'deviceStatusLabel', label: '设备状态', canOrder: false },
        { prop: 'malfunctionTime', label: '设备故障时间', minWidth: '140' },
        { prop: 'expectedTime', label: '期望修复时间', minWidth: '140' },
        { prop: 'typeName', label: '故障类型', canOrder: false },
        { prop: 'emergencyDegreeName', label: '紧急程度', canOrder: false },
        { prop: 'reportPersonName', label: '报修人员', canOrder: false },
        { prop: 'phenomenon', label: '故障现象', canOrder: false }
      ],
      defaultSearchColumns: [// 搜索区常规字段（初始值）
        { prop: 'searchValue', label: '模糊搜索', type: 'input' },
        // { prop: 'code', label: '报修单号', type: 'input' },
        { prop: 'bookId', label: '报修设备', type: 'select', optionsName: 'repairs' },
        { prop: 'statusList', label: '报修单状态', type: 'select-multiple', optionsName: 'equipmentInfoOptions' },
        { prop: 'deviceStatusCode', label: '设备状态', type: 'dict', optionsName: 'equipmentStatus' },
        { prop: 'malfunctionTime', label: '设备故障时间', type: 'time' },
        { prop: 'expectedTime', label: '期望修复时间', type: 'time' },
        // { prop: 'typeName', label: '故障类型', type: 'input' },
        { prop: 'emergencyDegree', label: '紧急程度', type: 'select', optionsName: 'emergencyDegreeOptions' }
        // { prop: 'reportPersonName', label: '报修人员', type: 'input' },
        // { prop: 'phenomenon', label: '故障现象', type: 'input' }

      ],
      emergencyDegreeOptions: [
        { value: undefined, label: '全部' },
        { value: 0, label: '轻微' },
        { value: 1, label: '重要' },
        { value: 2, label: '紧急' }
      ],

      equipmentStatus: [],
      repairs: [],
      operationData: [ // 右上角的操作按钮
        { label: '新增', perm: 'device:repairReport:add' },
        // { label: '批量操作', perm: '' },
        // { label: '批量删除', perm: 'device:repairReport:delete' },
        { label: '页面配置', perm: '' }
      ],
      equipmentInfoOptions: [
        { value: '1', label: '报修' },
        { value: '2', label: '待维修' },
        { value: '4', label: '维修中' },
        { value: '3', label: '已完成' }
      ],
      showKey: {
        type: 'column_two',
        keys: [
          { type: 'form', name: '报修单号', key: 'code' },
          { type: 'form', name: '报修设备', key: 'bookName' },
          { type: 'form', name: '设备状态', key: 'deviceStatusLabel' },
          { type: 'form', name: '设备故障时间', key: 'malfunctionTime' },
          { type: 'form', name: '报修人员', key: 'reportPersonName' },
          { type: 'form', name: '期望修复时间', key: 'expectedTime' },
          { type: 'form', name: '故障类型', key: 'typeName' },
          { type: 'form', name: '紧急程度', key: 'emergencyDegreeName' },
          { type: 'file', name: '故障情况拍照', fileName: 'docName', fileRealName: 'docRealName', fileUrl: 'docUrl' },
          { type: 'file', name: '附件', fileName: 'fileName', fileRealName: 'fileRealName', fileUrl: 'fileUrl' },
          { type: 'form', name: '故障现象', key: 'phenomenon', length: 'double' },
          { type: 'form', name: '维修人员', key: 'repairPersonName' },
          { type: 'br' },
          { type: 'form', name: '计划修复时间范围', key: 'planTime', length: 'double' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'notificationId'
    ])
  },
  watch: {
    notificationId(newValue, oldValue) {
      if (newValue !== oldValue && newValue !== 0) {
        const rowId = { id: this.notificationId }
        this.examine(rowId)
      }
    }
  },
  mounted() {
    this.getEquipmentStatus()
    this.getName()
    if (this.notificationId !== 0) {
      const rowId = { id: this.notificationId }
      this.examine(rowId)
    }
    if (this.$store.state.order.contendId) {
      this.examine({ id: this.$store.state.order.contendId })
      this.$store.state.order.contendId = ''
    }
  },
  methods: {
    cellStyle({ row, column }) {
      if (column.property == 'code') {
        return {
          cursor: 'pointer',
          color: '#387bfc'
        }
      }
    },
    cellClick(row, column, cell) {
      switch (column.property) {
        case 'code':
          this.examine(row)
          break
      }
    },
    getEquipmentStatus() {
      getDictSelects('state').then(res => {
        this.equipmentStatus = res.data
      })
    },
    getList() {
      list(this.pageData).then(res => {
        this.tableData = res.data
        this.total = res.total
        this.$nextTick(() => {
          this.setSelectRow(this)// 设置行选中
          this.tableHeight = this.$refs.table_container.offsetHeight
        })
      })
    },
    getName() {
      getEquipmentList1({ status: 1 }).then(res => {
        const cacheData = []
        res.data.forEach(item => {
          cacheData.push({
            value: item.id,
            label: item.deviceName
          })
        })
        this.repairs = cacheData
      })
    },
    refresh() {
      this.pageData.pageNum = 1
      this.getList()
    },
    examine(row) {
      getInfo(row[this.mainId]).then(res => {
        this.$layer.iframe({// 弹框
          content: {
            content: equipmentShow, // 传递的组件对象
            parent: this, // 当前的vue对象
            data: {
              showData: res.data,
              showKey: this.showKey
            }// props
          },
          area: ['1320px', '700px'],
          title: '查看设备报修', // 标题
          shadeClose: false // 点击遮罩是否关闭
        })
      })
      this.$store.dispatch('notification/changeId', 0)
    },
    // edit(row) {
    //   this.$layer.iframe({// 弹框
    //     content: {
    //       content: AddOrUpdate, // 传递的组件对象
    //       parent: this, // 当前的vue对象
    //       data: {
    //         id: row[this.mainId]
    //       }// props
    //     },
    //     area: ['880px', '700px'],
    //     title: '编辑设备报修', // 标题
    //     shadeClose: false // 点击遮罩是否关闭
    //   })
    // },
    add() {
      this.$layer.iframe({// 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {}// props
        },
        area: ['1320px', '700px'],
        title: '新增设备报修', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    deleteOne(row) {
      const _this = this
      this.$confirm('确定要删除吗', '删除确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        remove(row[_this.mainId]).then(res => {
          _this.$message({
            message: res.msg,
            type: 'success'
          })
          _this.getList()
        })
      }).catch(() => {})
    },
    // 当前页的删除接口
    deleteMethod(deleteIds) {
      return remove(deleteIds)
    }
  }
}
</script>

<style lang="less" scoped>
.accessory {
  color: #387bfc
}
/deep/.is-center {
  width: 60px !important;
}
</style>
